/**
 * @FileName: ProductOrderService.java
 * @Author
 * @Description:
 * @Date 2016年5月23日 下午5:13:13
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.kit.StrKit;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.util.SystemConfigUtil;

public class KeepMobileService{
    private static final Logger           logger       = LoggerFactory.getLogger(KeepMobileService.class);
    private String                        dprUrlPrefix = SystemConfigUtil.getSystemConfig().getCmccHostPrefix();

    public static final KeepMobileService service      = new KeepMobileService();

    /**
     * @Title: getSqlSelect
     * @Description:报表每行显示的信息，店员手机号，店员姓名，工号，所属机构读取的快照，其他从memberorg取
     * @return String
     */
    public String getSqlSelect(){
        String sql = " select o.*," + InterfaceUtil.getCitySql("o.user_org_id") + " as city, "
                + InterfaceUtil.getAreaSql("o.user_org_id") + " as area, "
                + " org.NumCode, org.DisplayName,  (select class_name from organization_class where enabled = '1' and id = org.webSiteId) as sitetype,  "
                + "DATE_FORMAT(o.opt_time,'%Y-%m-%d %T') as time,m.user_name,o.user_name as staff_name";
        return sql;
    }

    public String getSqlFilter(ConcurrentHashMap<String, Object> searchInfo){
        String sqlExceptSelect = " from keep_mobile_order o left join member_org m on o.user_id=m.id left join organization org on o.user_org_id=org.id where 1=1 ";

        for(Entry<String, Object> item : searchInfo.entrySet()){
            sqlExceptSelect += appendQueryFilter(item);
        }
        sqlExceptSelect += " ORDER BY o.opt_time DESC";

        return sqlExceptSelect;
    }

    private Object getOrgLimit(String orgId){
        String sql = " and o.user_org_id in("
                + InterfaceUtil.getChlidOrgIdSql(Integer.parseInt(orgId), Constant.MAX_ORG_LEVEL, true) + ") ";
        return sql;
    }

    /**
     * @Title: buildQuery
     * @Description:追加查询条件
     */
    private String appendQueryFilter(Entry<String, Object> item){
        StringBuilder sb = new StringBuilder();
        Object valueObj = item.getValue();
        String value = "";
        String key = item.getKey();
        if(null != valueObj){
            value = String.valueOf(valueObj);
        }
        switch(key){
            // case "org_id":
            // sb.append(getSubOrgIdFilter(value));
            // break;
            case "order_id":
                sb.append(getAndLikeFilter("o.order_id", value));
                break;
            case "mobile":
                sb.append(getAndLikeFilter("o.mobile", value));
                break;
            case "user_phone":
                sb.append(getAndLikeFilter("o.user_phone", value));
                break;
            case "city_in":
                sb.append(getAndLikeFilter("o.city_name_in", value));
                break;
            case "cmcc_id":
                // sb.append(String.format(" AND ( ( %s LIKE '%%%s%%') Or ( %s
                // LIKE '%%%s%%') ) ", "o.boss_no", value,
                // "sp.order_id", value));

                sb.append(getAndLikeFilter("o.boss_no", value));
                //
                // sb.append(getAndLikeFilter("sp.order_id", value));
                // System.out.println("subsql:" +
                // getAndLikeFilter("sp.thirdpart_id", value));
                break;
            case "staff_name":
                sb.append(getAndLikeFilter("o.user_name", value));
                break;
            // case "pay_plat":
            // if(!value.equals("-1")){
            // sb.append(getPayPlatFilter(value));
            // }
            // break;
            // case "pay_status":
            // JSONArray valueJA = JSONArray.parseArray(value);
            // sb.append(getPayStatusFilter("o.pay_status", valueJA));
            // break;
            // case "order_status":
            // if(!value.equals("-1")){
            // sb.append(getOrderStatusFilter(value));
            // }
            // break;

            case "start_date":
                sb.append(getStartTimeFilter(value));
                break;
            case "end_date":
                sb.append(getEndTimeFilter(value));
                break;
            case "org_id":
                sb.append(getOrgLimit(value));
                break;
            // case "order_type":
            // sb.append(getAndLikeFilter("o.type", value));
            // break;
            // case "creator":
            // sb.append(getCreatorFilter(value));
            // break;
            default:
                break;
        }
        return sb.toString();
    }

    /**
     * @Title: getPayStatusFilter
     * @Description:TODO
     * @param string
     * @param valueJA
     * @return Object
     */
    private Object getPayStatusFilter(String keyName, JSONArray valueJA){
        String sql = "";
        boolean isAll = false;
        for(int i = 0, len = valueJA.size(); i < len; i++){
            String va = (String)valueJA.get(i);
            if(i == 0){
                if(!"-1".equals(va)){
                    sql += " AND (" + keyName + " = " + va;
                } else{
                    isAll = true;
                }
            } else{
                if(!"-1".equals(va)){
                    if(isAll){
                        sql += " AND (" + keyName + " = " + va;
                        isAll = false;
                    } else{
                        sql += " OR " + keyName + " = " + va;
                    }
                }
            }
            if((i == len - 1) && !isAll){
                sql += ")";
            }

        }

        return StrKit.notBlank(sql) ? String.format(sql) : "";
    }

    /**
     * @Title: getEndTimeFilter
     * @Description:获取开始时间查询条件
     */
    private String getEndTimeFilter(String value){
        return String.format(" AND o.opt_time <= '%s 23:59:59' ", value);
    }

    /**
     * @Title: getStartTimeFilter
     * @Description:获取结束时间查询条件
     */
    private String getStartTimeFilter(String value){
        return String.format(" AND o.opt_time >= '%s 00:00:00' ", value);
    }

    /**
     * @Title: getAndLikeFilter
     * @Description:获取KeyName和value模糊查询条件
     */
    private String getAndLikeFilter(String keyName, String value){
        return String.format(" AND %s LIKE '%%%s%%' ", keyName, value);
    }
}
